<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="950" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px"
				 label-position="left">
			<el-form-item label="姓名" prop="doctor_name">
				<el-input v-model="form.doctor_name" placeholder="请输入医生姓名" clearable></el-input>
			</el-form-item>
			<el-form-item label="科室" prop="department_code">
				<el-input v-model="form.department_name" placeholder="请选择" @click="openDepartment" @clear="clearDepartment"
						  clearable></el-input>
			</el-form-item>
			<el-form-item label="职称" prop="job_title">
				<el-select v-model="form.job_title" filterable style="width: 100%">
					<el-option v-for="item in job_title_list" :key="item.job_title" :label="item.job_title_name" :value="item.job_title"/>
				</el-select>
			</el-form-item>
			<el-form-item label="手机号" prop="mobile">
				<el-input v-model="form.mobile" placeholder="请输入完整手机号" autocomplete="off" clearable></el-input>
			</el-form-item>
			<template v-if="mode=='edit'">

			</template>
			<el-form-item label="擅长领域" prop="skill">
				<el-input v-model="form.skill" :rows="2" type="textarea" placeholder="请填写医生擅长领域" clearable></el-input>
			</el-form-item>
			<el-form-item label="执业经历" prop="job_his">
				<el-input v-model="form.job_his" :rows="2" type="textarea" placeholder="例如：医生个人学习经历、执业经历介绍、高光时刻介绍、擅长病情介绍" clearable></el-input>
			</el-form-item>
			<el-form-item label="执业证编号" prop="job_no">
				<el-input v-model="form.job_no" clearable></el-input>
			</el-form-item>
<!--			<el-form-item label="工作状态" prop="work_status">-->
<!--				<el-select v-model="form.work_status" filterable style="width: 100%">-->
<!--					<el-option v-for="item in work_status_list" :key="item.work_status" :label="item.work_status_name" :value="item.work_status"/>-->
<!--				</el-select>-->
<!--			</el-form-item>-->
<!--			<el-form-item label="出诊状态" prop="job_status">-->
<!--				<el-select v-model="form.job_status" filterable style="width: 100%">-->
<!--					<el-option v-for="item in job_status_list" :key="item.job_status" :label="item.job_status_name" :value="item.job_status"/>-->
<!--				</el-select>-->
<!--			</el-form-item>-->
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
	<department-popup ref="departmentPopup" @success="departmentPopupSuccess" @closed="departmentPopupShow = false"></department-popup>
</template>

<script>
	import {verifyPhone} from '@/utils/verificate.js';
	import departmentPopup from './department.vue';

	export default {
		components: {departmentPopup},
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新建医生',
					edit: '编辑医生',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					doctor_id: "",
					doctor_name: "",
					mobile: "",
					job_title: "",
					skill: "",
					job_his: "",
					job_no: "",
					// work_status: "",
					// job_status: "",
					department_code:"",
					department_name:"",
				},
				job_title_list: [],
				work_status_list: [],
				job_status_list: [],
				//验证规则
				rules: {
					doctor_name: [
						{required: true, message: '请输入姓名', trigger: 'blur'}
					],
					department_code: [
						{required: true, message: '请选择科室', trigger: 'blur'}
					],
					mobile: [
						{required: true, message: '请输入手机号', trigger: 'blur'},
						{validator: verifyPhone, trigger: "blur"}
					],
					job_title: [
						{required: true, message: '请选择职称', trigger: 'change'}
					],
					job_no: [
						{required: true, message: '请输入执业证编号', trigger: 'blur'}
					],
				},
			}
		},
		mounted() {
			this.getSearchDict();
		},
		methods: {
			getSearchDict() {
				this.$API['admin.doctor'].getSearchDict.get().then((res) => {
					if (res.code == 0) {
						this.job_title_list = res.data.job_title_list;
						this.work_status_list = res.data.work_status_list;
						this.job_status_list = res.data.job_status_list;
					}
				}).catch((msg) => {
					this.$alert(msg, "提示", {type: 'error'})
				})
			},
			//显示
			open(mode = 'add') {
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit() {
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						this.$API['admin.doctor'].store.post(this.form)
							.then((res) => {
								if (res.code == 0) {
									this.$emit('success', this.form, this.mode)
									this.$message.success("操作成功")
								} else {
									this.$alert(res.message, "提示", {type: 'error'})
								}
							})
							.catch((msg) => {
								this.$alert(msg, "提示", {type: 'error'})
							})
							.finally(() => {
								this.isSaveing = false;
							});
					} else {
						return false;
					}
				})
			},
			//表单注入数据
			setData(data) {
				this.form.doctor_id = data.doctor_id
				this.form.doctor_name = data.doctor_name
				this.form.mobile = data.mobile
				this.form.job_title = data.job_title
				this.form.skill = data.skill
				this.form.job_his = data.job_his
				this.form.job_no = data.job_no
				this.form.work_status = data.work_status
				this.form.job_status = data.job_status
				this.form.department_code = data.department_code
				this.form.department_name = data.department_name
				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				//Object.assign(this.form, data)
			},
			//所属科室弹窗
			openDepartment() {
				this.departmentPopupShow = true;
				this.$refs.departmentPopup.open()
			},
			//科室选择成功后
			departmentPopupSuccess(selection) {
				if (!selection){
					this.$alert('请选择科室', '提示', { type: 'error' });
					return false;
				}
				if(selection.length != 1){
					this.$alert('只能选择一个科室', '提示', { type: 'error' });
					return false;
				}
				let department_code = '';
				let department_name = '';
				selection.forEach(val => {
					department_code = val.department_code;
					department_name = val.department_name;
				});
				this.form.department_code = department_code;
				this.form.department_name = department_name;
				this.$refs.departmentPopup.close();
			},
			//清除科室
			clearDepartment(){
				this.form = {
					department_code:'',
					department_name:''
				}
			},
		}
	}
</script>

<style>
</style>
